package cn.kgc.controller;


import cn.kgc.dto.EvaluateDto;
import cn.kgc.dto.MalfunctionDto;
import cn.kgc.dto.Result;
import cn.kgc.dto.UsersDto;
import cn.kgc.pojo.Evaluate;
import cn.kgc.service.EvaluateService;
import cn.kgc.service.MalfunctionService;
import cn.kgc.service.UsersService;
import cn.kgc.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评估信息 前端控制器
 * </p>
 *
 * @author psq
 * @since 2021-08-05
 */
@Api("评估信息")
@RestController
@RequestMapping("/api/evaluate")
public class EvaluateController {
    private final EvaluateService evaluateService;
    private final MalfunctionService malfunctionService;
    private final UsersService usersService;

    public EvaluateController(EvaluateService evaluateService, MalfunctionService malfunctionService, UsersService usersService) {
        this.evaluateService = evaluateService;
        this.malfunctionService = malfunctionService;
        this.usersService = usersService;
    }

    @ApiOperation("specList")
    @GetMapping("specList")
    public Result<List<MalfunctionDto>> findSpecListByModelId(Long modelId) {
        List<MalfunctionDto> list = malfunctionService.findSpecListByModelId(modelId);
        return ResultUtils.returnDataSuccess(list);
    }

    @PostMapping("assess")
    public Result<EvaluateDto> assess(Long modelId, String optionIds, HttpServletRequest request) {
        // 1.先获取token
        String token = request.getHeader("token");
        // 2.通过 token 获取用户信息
        UsersDto user = usersService.getLoginByToken(token);

        // 3.调用评估方法，进行评估
        Evaluate evaluate = evaluateService.assess(modelId, optionIds, user.getId());
        return ResultUtils.returnDataSuccess(evaluate);
    }

    @GetMapping("{id}/assessInfo")
    public Result<EvaluateDto> assessInfo(@PathVariable Long id) {
        EvaluateDto evaluateDto = evaluateService.assessInfo(id);
        return ResultUtils.returnDataSuccess(evaluateDto);
    }
}

